package com.example.hospital_208.controller;

import com.example.hospital_208.po.PatientInfo;
import com.example.hospital_208.service.PatientInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/patient-infos")
@Tag(name = "患者信息管理接口", description = "患者信息的增删改查及统计")
public class PatientInfoController {
    private final PatientInfoService patientInfoService;

    public PatientInfoController(PatientInfoService patientInfoService) {
        this.patientInfoService = patientInfoService;
    }

    // 获取所有患者信息
    @GetMapping
    @Operation(summary = "查询所有患者", description = "获取医院所有患者信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(implementation = PatientInfo.class)))
    })
    public List<PatientInfo> getAllPatients() {
        return patientInfoService.listPatientInfos();
    }

    // 根据ID查询患者
    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询患者", description = "通过患者ID获取详细信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(implementation = PatientInfo.class))),
            @ApiResponse(responseCode = "404", description = "患者不存在")
    })
    public PatientInfo getPatientById(
            @PathVariable("id")
            @Parameter(description = "患者ID", example = "110101198505055678", required = true)
            String id) {
        return patientInfoService.getPatientInfoById(id);
    }

    // 新增患者
    @PostMapping
    @Operation(summary = "新增患者", description = "向系统中添加患者信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "添加成功"),
            @ApiResponse(responseCode = "400", description = "参数错误")
    })
    public String addPatient(@RequestBody PatientInfo patientInfo) {
        int result = patientInfoService.addPatientInfo(patientInfo);
        return result > 0 ? "患者添加成功" : "患者添加失败";
    }

    // 修改患者信息
    @PutMapping
    @Operation(summary = "修改患者信息", description = "更新已有患者的信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "修改成功"),
            @ApiResponse(responseCode = "400", description = "参数错误或患者不存在")
    })
    public String updatePatient(@RequestBody PatientInfo patientInfo) {
        int result = patientInfoService.editPatientInfo(patientInfo);
        return result > 0 ? "患者信息更新成功" : "患者信息更新失败";
    }

    // 删除患者
    @DeleteMapping("/{id}")
    @Operation(summary = "删除患者", description = "根据患者ID删除患者信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "删除成功"),
            @ApiResponse(responseCode = "400", description = "参数错误或患者存在关联数据")
    })
    public String deletePatient(@PathVariable("id") String id) {
        int result = patientInfoService.removePatientInfo(id);
        return result > 0 ? "患者删除成功" : "患者删除失败";
    }

    // 按姓名模糊查询患者列表
    @GetMapping("/name/{name}")
    @Operation(summary = "按姓名查询患者", description = "通过姓名模糊查询患者信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(implementation = PatientInfo.class)))
    })
    public List<PatientInfo> getPatientsByName(
            @PathVariable("name")
            @Parameter(description = "患者姓名", example = "张三", required = true)
            String name) {
        return patientInfoService.listPatientInfosByName(name);
    }

    // 按姓名统计患者人数
    @GetMapping("/count/name/{name}")
    @Operation(summary = "统计同名患者人数", description = "统计指定姓名的患者数量")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "统计成功",
                    content = @Content(schema = @Schema(type = "integer")))
    })
    public int countPatientsByName(
            @PathVariable("name")
            @Parameter(description = "患者姓名", example = "张三", required = true)
            String name) {
        return patientInfoService.countPatientsByName(name);
    }

    // 根据 user_id 查询患者信息
    @GetMapping("/user/{user_id}")
    @Operation(summary = "根据 user_id 查询患者信息", description = "通过 user_id 查询患者的详细信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(implementation = PatientInfo.class))),
            @ApiResponse(responseCode = "404", description = "患者信息不存在")
    })
    public PatientInfo getPatientInfoByUserId(
            @PathVariable("user_id")
            @Parameter(description = "用户 ID", example = "1", required = true)
            Integer userId) {
        return patientInfoService.getPatientInfoByUserId(userId);
    }

    // 新增：根据 patient_infoid 查询患者信息
    @GetMapping("/infoid/{patient_infoid}")
    @Operation(summary = "根据 patient_infoid 查询患者信息", description = "通过 patient_infoid 查询患者的详细信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(implementation = PatientInfo.class))),
            @ApiResponse(responseCode = "404", description = "患者信息不存在")
    })
    public PatientInfo getPatientInfoByInfoid(
            @PathVariable("patient_infoid")
            @Parameter(description = "患者信息主键 ID", example = "1", required = true)
            Integer patient_infoid) {
        return patientInfoService.getPatientInfoByInfoid(patient_infoid);
    }
    // 新增：根据 patient_id 查询患者所有信息
    @GetMapping("/patientid/{patient_id}")
    @Operation(summary = "根据 patient_id 查询患者所有信息", description = "通过 patient_id 查询患者的详细信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(implementation = PatientInfo.class))),
            @ApiResponse(responseCode = "404", description = "患者信息不存在")
    })
    public PatientInfo getPatientInfoByPatientId(
            @PathVariable("patient_id")
            @Parameter(description = "患者 ID", example = "110101198505055678", required = true)
            String patient_id) {
        return patientInfoService.getPatientInfoByPatientId(patient_id);
    }
}